<?php defined('SYSPATH') or die('No direct script access.');

class Model_Category extends ORM
{ 
    protected $_reload_on_wakeup = false; 
    
    public function get_categories_urls()
    {
        return array(
            362 => 'cat-362',
            414 => 'cat-414',
            415 => 'phones',
            392 => 'ebooks',
            363 => 'gps',
            420 => 'bluetooth',
            421 => 'watches',
            417 => 'cordedphones',
            418 => 'radiophones',
            402 => 'cat-402',
            403 => 'tv',
            409 => 'dvd',
            393 => 'cat-393',
            396 => 'camera',
            394 => 'lenses',
            395 => 'flashlights',
            375 => 'video',
            399 => 'photoframes',
            400 => 'tripods',
            366 => 'cat-366',
            388 => 'mp3-players',
            385 => 'headphones',
            369 => 'acoustics',
            370 => 'acoustic-kits',
            371 => 'stereos',
            373 => 'amplifiers',
            376 => 'home-cinemas',
            383 => 'dictaphones',
            384 => 'receivers',
            386 => 'portable-speakers',
            387 => 'radios',
            391 => 'mediaplayers',
            236 => 'cat-236',
            715 => 'cat-715',
            273 => 'notebooks',
            275 => 'tablets',
            265 => 'monitors',
            276 => 'printers',
            290 => 'scanners',
            246 => 'cat-246',
            247 => 'power-supplies',
            248 => 'videocards',
            249 => 'hdd',
            250 => 'soundcards',
            251 => 'cases',
            252 => 'coolers',
            253 => 'motherboard',
            254 => 'memory',
            255 => 'drives',
            256 => 'cpu',
            257 => 'cat-257',
            258 => 'keyboards',
            261 => 'joysticks',
            266 => 'cat-266',
            267 => 'tv-tuners',
            268 => 'webcams',
            269 => 'speakers',
            270 => 'computer-headsets',
            281 => 'cat-281',
            282 => 'voip',
            283 => 'routers',
            284 => 'modems',
            288 => 'wi-fi',
            238 => 'cat-238',
            237 => 'flash-drives',
            239 => 'laptop-bags',
            240 => 'ups',
            241 => 'memory-cards',
            25 => 'cat-25',
            61 => 'cat-61',
            62 => 'airgrills',
            63 => 'blenders',
            65 => 'ovens',
            68 => 'coffee-makers',
            69 => 'coffee-grinders',
            70 => 'kitchen-scales',
            71 => 'food-processors',
            72 => 'microwaves',
            73 => 'mixers',
            74 => 'meat-grinder',
            75 => 'steamers',
            76 => 'cookers',
            77 => 'dishwashers',
            78 => 'juicers',
            79 => 'toasters',
            81 => 'bread-machines',
            82 => 'refrigerators',
            83 => 'elektrovarki',
            84 => 'kettles',
            26 => 'cat-26',
            27 => 'vacuums',
            28 => 'washers',
            29 => 'irons',
            30 => 'water-filters',
            34 => 'sewing-machines',
            38 => 'cat-38',
            39 => 'shearers',
            40 => 'scales',
            41 => 'hairdryers',
            43 => 'hair-straighteners',
            49 => 'toothbrushes',
            50 => 'razors',
            51 => 'epilators',
            101 => 'cat-101',
            102 => 'fans',
            104 => 'water-heaters',
            105 => 'conditioners',
            107 => 'heaters',
            108 => 'air-cleaners',
            109 => 'weather-stations',
            113 => 'split-systems',
            116 => 'cat-116',
            117 => 'cat-117',
            118 => 'lawn-mowers',
            120 => 'pressurewashers',
            179 => 'cat-179',
            181 => 'baths',
            185 => 'cranes',
            210 => 'cat-210',
            211 => 'saws',
            212 => 'drills',
            213 => 'jigsaws',
            215 => 'perforators',
            216 => 'polishing',
            217 => 'pumps',
            716 => 'cat-716',
            304 => 'cat-304',
            308 => 'bicycles',
            312 => 'tents',
            315 => 'sleeping-bags',
            327 => 'exercise-bikes',
            329 => 'treadmills',
            335 => 'elliptical-trainers',
            295 => 'cat-295',
            298 => 'cradles',
            712 => 'kid-bikes',
            713 => 'car-seats',
            714 => 'prams',
            1 => 'cat-1',
            3 => 'tires',
            11 => 'camcoders',
            21 => 'autorecorders',
        );
    }
    
    public function get_category_url($category_id)
    {
        $categories = $this->get_categories_urls();
        if (isset($categories[$category_id])) {
            return $categories[$category_id];   
        }
        return null;
    }
    
    public function get_category($url)
    {
        return ORM::factory('category')
                ->where('url', '=', $url)
                ->find();
    }
    
    public function get_main_categories()
    {
        if ($categories = Cache::instance()->get('main_categories')) {
            return $categories;
        }
        
        $main_cats = array(
            415 => 362, 403 => 362, 396 => 362, 392 => 362, 388 => 362, 385 => 362,
            275 => 236, 273 => 236, 256 => 236, 248 => 236, 249 => 236, 253 => 236,
            82  => 25,  27  => 25,  28  => 25,  75  => 25,  72  => 25,  77  => 25,
            181 => 116, 185 => 116, 212 => 116,
            308 => 716, 329 => 716, 713 => 716, 714 => 716, 3   => 716, 11  => 716,
        );
        
        $_first_row = $this->get_top_categories();
        $first_row = array();
        
        foreach ($_first_row as $category) {
            $first_row[$category->id] = $category->as_array();
            $first_row[$category->id]['items']  = array();  
        }
        
        $second_row = $this->_get_categories_query()
                    ->where('id', 'IN', array_keys($main_cats))
                    ->find_all();
        
        foreach ($second_row as $category) {
            $first_row[$main_cats[$category->id]]['items'][] = $category->as_array();
        }
        
        Cache::instance()->set('main_categories', $first_row);
        return $first_row;
    }
    
    public function get_categories_tree()
    {
        if ($categories = Cache::instance()->get('categories_tree')) {
            return $categories;
        }
        
        // Максимум 3 уровня вложенноси, не придумываем ничего универсального
        $_first_row = $this->get_top_categories();
        $first_row = array();
        
        $ids = array();
        foreach ($_first_row as $category) {
            $ids[] = $category->id; 
            $first_row[$category->id] = $category->as_array();
            $first_row[$category->id]['active'] = false;
            $first_row[$category->id]['items']  = array();  
        }
        $_second_row = $this->_get_categories_query()
                    ->where('parent_id', 'IN', $ids)
                    ->find_all();
        $second_row = array();
                    
        $ids = array();
        foreach ($_second_row as $category) {
            $ids[] = $category->id; 
            $second_row[$category->id] = $category->as_array();
            $second_row[$category->id]['active'] = false;
            $second_row[$category->id]['items']  = array();
        }
        $third_row = $this->_get_categories_query()
                    ->where('parent_id', 'IN', $ids)
                    ->find_all()
                    ->as_array();
                    
        foreach ($third_row as &$category) {
            $category = $category->as_array();
            $category['active'] = true;
            $second_row[$category['parent_id']]['items'][] = $category;   
        }
        
        foreach ($second_row as &$category) {
            $first_row[$category['parent_id']]['items'][] = $category;    
        }
        
        Cache::instance()->set('categories_tree', $first_row);
        return $first_row;        
    }
    
    public function get_top_categories()
    {
        if ($categories = Cache::instance()->get('top_categories')) {
            return $categories;
        }
        $categories = $this->_get_categories_query()->where('parent_id', '=', 0)->find_all(); 
        Cache::instance()->set('top_categories', Model_Main::get_array($categories));
        return $categories;
    } 
    
    public function get_categories()
    {
        if ($categories = Cache::instance()->get('categories_'.$this->id)) {
            return $categories;
        }
        $categories = $this->_get_categories_query()->where('parent_id', '=', $this->id)->find_all();
        Cache::instance()->set('categories_'.$this->id, Model_Main::get_array($categories));
        return $categories;  
    }
    
    protected function _get_categories_query()
    {
        return ORM::factory('category');  
    }
    
    public function get_breadcrumbs()
    {
        // TODO cache
        $breadcrumbs = array();
        $category_id = $this->parent_id;
        $old_category_id = $category_id;
        
        while ($category_id) {
            $category = ORM::factory('category', $category_id);
            $breadcrumbs[] = $category;   
            
            $category_id = $category->parent_id;
        }
        
        $breadcrumbs = array_reverse($breadcrumbs);
        return $breadcrumbs;    
    }
}